/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tdtest.controls;

import java.util.HashMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import res.ImageLoader;
import tdtest.action.AddListItem;
import tdtest.action.RemoveListItem;
import tdtest.data.DataObject;
import tdtest.data.TDList;

/**
 *
 * @author sutter
 */
public class ControlList<T extends DataObject> extends GridPane implements DataControl<TDList<T>> {
	
	private TDList<T> list;
	private Label nameLabel;
	private Button addButton;
	private GridPane contentList;
	private ControlFactory<T> controlFactory;
	private Image deleteImage;
	
	public ControlList(TDList<T> list, ControlFactory<T> controlFactory) {
		getStyleClass().add("control-list");
		this.controlFactory = controlFactory;
		init(list);
	}
	
	@Override
	public TDList<T> getData() {
		return list;
	}
	
	@Override
	public void onUpdate() {
		//remove all control objects and add to a map
		HashMap<DataObject,Node> controlObjects = new HashMap<>();
		for(Node node:contentList.getChildren()) {
			if(node instanceof DataControl) {
				controlObjects.put(((DataControl)node).getData(), node);
			}
		}
		contentList.getChildren().clear();
		
		//cycle through data objects and insert, creating if needed
		int index = 0;
		for(T data:list.groupItems) {
			Node node = controlObjects.get(data);
			if(node == null) {
				node = createListNode(data);
			}
			insertListNode(data,node,index++);
		}
		
		//discard missing items.
	}
	
	@Override
	public void onDelete() {
		
	}
	
	private void init(TDList<T> list) {
		this.list = list;
		list.addOnChanagedListener(this);
		
		ColumnConstraints column0 = new ColumnConstraints();
		this.getColumnConstraints().addAll(column0);
		
		ColumnConstraints column1 = new ColumnConstraints(10,10,Double.MAX_VALUE);
		column1.setHgrow(Priority.ALWAYS);
		this.getColumnConstraints().addAll(column1);
		
		nameLabel = new Label();
		nameLabel.getStyleClass().add("name-text");
		add(nameLabel, 0, 0);
		
		addButton = new Button();
		addButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override public void handle(ActionEvent e) {
				AddListItem<T> action = new AddListItem<>(ControlList.this.list,controlFactory);
				action.run();
			}
		});
		Image addImage = ImageLoader.getImage("add.png");
		addButton.setGraphic(new ImageView(addImage));
		addButton.getStyleClass().add("add-button");
		addButton.getStyleClass().add("image-button");
		add(addButton, 1,0);
		
		contentList = new GridPane();
		column0 = new ColumnConstraints();
		contentList.getColumnConstraints().addAll(column0);
		column1 = new ColumnConstraints(10,10,Double.MAX_VALUE);
		column1.setHgrow(Priority.ALWAYS);
		contentList.getColumnConstraints().addAll(column1);
		contentList.getStyleClass().add("list-entry-pane");
		add(contentList,0,1,2,1);
		
		nameLabel.setText(list.name);
		
		deleteImage = ImageLoader.getImage("delete.png");
		
		int index = 0;
		for(T item:list.groupItems) {
			Node node = createListNode(item);
			insertListNode(item,node,index++);
		}
	}
	
	public void addNameClass(String titleClass) {
		nameLabel.getStyleClass().add(titleClass);
	}
	
	private Node createListNode(T data) {
		return controlFactory.createControl(data);
	}
	
	private void insertListNode(final T data, Node node, int index) {
		VBox vb = new VBox();
		Button button = new Button();
		button.setOnAction(new EventHandler<ActionEvent>() {
			@Override public void handle(ActionEvent e) {
				RemoveListItem<T> action = new RemoveListItem<>(list,data);
				action.run();
			}
		});
		button.setGraphic(new ImageView(deleteImage));
		button.getStyleClass().add("close-button");
		button.getStyleClass().add("image-button");
		vb.getChildren().add(button);
		vb.setAlignment(Pos.TOP_LEFT);
		contentList.add(vb,0,index);
		contentList.add(node,1,index);
	}
}
